package edu.ilstu.mind.ise.ccp.logger.gui;

import java.util.Map;

import edu.ilstu.mind.ise.ccp.config.ConfigParserUtil;
import edu.ilstu.mind.ise.ccp.logger.Logger;
import edu.ilstu.mind.ise.ccp.logger.LoggerFactory;
import edu.ilstu.mind.ise.ccp.logger.LoggerLevel;

public class GuiLoggerFactory implements LoggerFactory {
	
	private static final int DEFAULT_WIDTH = 800;
	private static final int DEFAULT_HEIGHT = 600;
	private final TabbedLoggerGui gui;
	private final String PROP_KEY_LEVEL = "level";
	private final String PROP_KEY_PRIORITY_CONSOLES = "priorityConsoles";
	
	private final LoggerLevel level;
	
	public GuiLoggerFactory(LoggerLevel level, String[] priorityConsoles) {
		this.level = level;
		gui = new TabbedLoggerGui(DEFAULT_WIDTH, DEFAULT_HEIGHT, true, priorityConsoles);
	}
	
	public GuiLoggerFactory(Map<String, Object> props) {
		level = LoggerLevel.parse(ConfigParserUtil.getRequiredString(props, PROP_KEY_LEVEL));
		String priorityConsolesStr = ConfigParserUtil.getString(props, PROP_KEY_PRIORITY_CONSOLES);
		String[] priorityConsoles = priorityConsolesStr != null ? priorityConsolesStr.split(",") : new String[0];
		gui = new TabbedLoggerGui(DEFAULT_WIDTH, DEFAULT_HEIGHT, true, priorityConsoles);
	}

	@Override
	public Logger createLogger(String name) {
		return new GuiLogger(gui.getOrCreateConsoleTab(name), gui.getErrorTabWriter(), name, level);
	}
}